<?php

namespace App\Http\Controllers\Api\Web\Student;

use App\Http\Controllers\Common\ApiController;
use App\Models\Common\China;
use App\Models\Constant\StudentConstant;
use App\Models\Student\Student;
use App\Service\StudentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StudentController extends ApiController {

    private $service;

    public function __construct() {
        $this->service = new StudentService(Student::class);
    }

    public function applyShow(Request $request) {
        $gender = [
            ['id'=>'1','value'=>'男'],
            ['id'=>'2','value'=>'女'],
        ];
        $family_is_support_adjust = [
            ['id'=>'1','value'=>'是'],
            ['id'=>'2','value'=>'否'],
            ['id'=>'3','value'=>'未沟通'],
        ];

        $english_level = [];
        foreach (StudentConstant::MAP_ENGLISH_LEVEL as $k=>$v) {
            $english_level[] = ['id'=>$k,'value'=>$v];
        }

        $major_classify = DB::table('major_classify')->get();
        $apply_postgraduate_major = DB::table('apply_postgraduate_major')->get();
        $college = DB::table('college')->get();

        $china = China::with('children')->get(['name','code','pcode']);
        $this->handleChildren($china);
        return responseSuccess(compact('major_classify','apply_postgraduate_major','english_level','family_is_support_adjust','gender','china','college'));

    }

    public function apply(Request $request) {
        $this->validate($request, [
            'xing'=>'required|regex:/[\x{4e00}-\x{9fa5}]/u',
            'ming'=>'required|regex:/[\x{4e00}-\x{9fa5}]/u',
            'gender'=>'required|min:1|max:2|Integer',
            'birthday'=>'required|date_format:Y-m-d',
            'phone'=>'required|regex:/^1[3456789]\d{9}$/',
            'email'=>'required|email:rfc,dns',
            'hu_prov_code'=>'required',
            'hu_city_code'=>'required',
            'family_is_support_adjust'=>'required|min:1|max:3|Integer',

            //学习背景
            'college_id'=>'required|Integer',
            'major_classify_id'=>'required|Integer',
            'major_name'=>'required',
            'join_college_year'=>'required|Integer',
            'end_college_year'=>'required|Integer',
            'english_level_id'=>'required|Integer',
            //'english_level_highest_point'=>'sometimes|numeric',
            'test_postgraduate_college_id'=>'required|Integer',
            'apply_postgraduate_major_id'=>'required|Integer',
            'test_postgraduate_major_classify_id'=>'required|Integer',
            'test_postgraduate_major_name'=>'required',

            //家庭信息
            /*'father_name'=>'required',
            'father_phone'=>'required|regex:/^1[3456789]\d{9}$/',
            'father_work'=>'required',
            'father_job'=>'required',
            'mather_name'=>'required',
            'mather_phone'=>'required|regex:/^1[3456789]\d{9}$/',
            'mather_work'=>'required',
            'mather_job'=>'required',*/

            //社会实践
            //'abroad_experience'=>'required',
            //'public_activity'=>'required'

        ],[
            'xing.regex'=>'姓 必须包含中文',
            'ming.regex'=>'名 必须包含中文',
            'phone.regex'=>'手机不合法',
        ],
            [
            'xing'=>'姓',
            'ming'=>'名',
            'hu_prov_code'=>'户籍所在 省',
            'hu_city_code'=>'户籍所在 市',
            'family_is_support_adjust'=>'家长是否支持你调剂海外院校',

            //学习背景
            'college_id'=>'本科院校',
            'major_classify_id'=>'本科专业大类',
            'major_name'=>'本科专业',
            'join_college_year'=>'入学年份',
            'end_college_year'=>'毕业年份',
            'english_level_id'=>'英语等级',
            'test_postgraduate_college_id'=>'你想申请的国外硕士专业',
            'apply_postgraduate_major_id'=>'考研目标院校',
            'test_postgraduate_major_classify_id'=>'考研目标专业大类',
            'test_postgraduate_major_name'=>'考研目标专业',
            'english_level_highest_point'=>'最高英语成绩',


            //家庭信息
            'father_name'=>'父亲姓名',
            'father_phone'=>'父亲电话',
            'father_work'=>'父亲工作单位',
            'father_job'=>'父亲职务',
            'mather_name'=>'母亲姓名',
            'mather_phone'=>'母亲电话',
            'mather_work'=>'母亲工作单位',
            'mather_job'=>'母亲职务',

            //社会实践
            //'abroad_experience'=>'出国经历',
            //'public_activity'=>'公益活动'
        ]);

        $info = $request->input();

        if(isset($info['english_level_highest_point']) && !is_numeric($info['english_level_highest_point'])){
            return responseFail('最高英语成绩必须是个数字！');
        }


        if(isset($info['abroad_experienceName'])){
            unset($info['abroad_experienceName']);
        }
        if(isset($info['public_activityName'])){
            unset($info['public_activityName']);
        }


        $this->service->add($info);
        return responseSuccess();
    }

    public function result(Request $request) {
        $name = $request->input('name');
        $phone = $request->input('phone');
        $this->validate($request, [
            'name'=>'required',
            'phone'=>'required|regex:/^1[3456789]\d{9}$/',
        ],[
            'phone.regex'=>'手机不合法',
        ],[
            'name'=>'姓名',
        ]);

        $res = $this->service->result($name, $phone);
        return responseSuccess($res,0,100000,[],$res['status']);
        /*if($res['status']){
            return responseSuccess($res);
        }else{
            return responseFail('',$res);
        }*/

    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $records = Student::where(compact('id'))->first();
        return responseSuccess($records->toArray(),1);
    }

    public function queryResult(Request $request) {
        $name = $request->input('name');
        $phone = $request->input('phone');
        $this->validate($request, ['name'=>'required','phone'=>'required']);

        $student = $this->service->lst(compact('phone'))->first();
        if(empty($student))return responseFail('未查找到有效结果！');



        return responseSuccess($student->toArray(),1);
    }
}
